/**
 * 用全名去查找能匹配的客户
 * @param self
 * @param tiyData
 * @returns {Array}
 */
export const userMatchList = (self, tiyData, userInfo) => {
  self.$Spin.show();
  tiyData.tStatus.allotModal.userInfo.id = userInfo.id;
  tiyData.tStatus.allotModal.userInfo.inviteCode = userInfo.inviteCode;
  tiyData.tStatus.allotModal.userInfo.fullName = userInfo.fullName;
  tiyData.tStatus.allotModal.userInfo.contactEmail = userInfo.contactEmail;
  tiyData.tStatus.allotModal.userInfo.isMatched = userInfo.isMatched > 0;

  tiyData.tParams.allotUserListParam.fullName = userInfo.fullName;
  tiyData.tParams.allotUserListParam.email = userInfo.contactEmail;
  self.$apiUser.userMatchList({
    "fullName":tiyData.tParams.allotUserListParam.fullName,
    "email":tiyData.tParams.allotUserListParam.email
  }).then(function (response) {
    self.$Spin.hide();
    if (response.success) {
      tiyData.tData.allotUserList.length = 0;
      response.result.forEach(
        function (item) {
          tiyData.tData.allotUserList.push(item);
        }
      );
      tiyData.tStatus.allotModal.isShow = true;
    } else {
      self.$Message.error(response.errorMessage);
    }
  });
};

/**
 * 匹配用户和目标客户
 * @param self
 * @param tiyData
 * @param index
 * @param afterFun 执行成功后回调函数
 */
export const userToTargetCustomer = (self, tiyData, index, afterFun) => {
  self.$Spin.show();
  let targetCustomerId = tiyData.tData.allotUserList[index].id
  self.$apiUser.userToTargetCustomer({
    "userId":tiyData.tStatus.allotModal.userInfo.id,
    "inviteCode":tiyData.tStatus.allotModal.userInfo.inviteCode,
    "targetCustomerId":targetCustomerId
  }).then(function (response) {
    self.$Spin.hide();
    if (response.success) {
      self.$Message.success("保存成功！")
      tiyData.tStatus.allotModal.isShow = false;
      tiyData.tStatus.allotModal.userInfo.id = "";
      tiyData.tStatus.allotModal.userInfo.inviteCode = "";
      tiyData.tStatus.allotModal.userInfo.targetCustomerId = "";
      if(afterFun){
        afterFun();
      }
    } else {
      self.$Message.error(response.errorMessage);
    }
  });
};

